<?php

namespace App\Console\Commands;

use App\Common\Response;
use App\Http\Controllers\WeChat\WechatController;
use App\Models\Product;
use App\Models\Progress;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;


class renewalService extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'renewal:Service';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '客户定时续费提醒任务';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
       try{
           $timeZone = "Asia/Shanghai"; //时区
           $Carbon = new Carbon();
           $time=date("Y-m-d H:i:s",time());
           $expiresAt =$Carbon::now()->addMinutes(1);
           $check     = Cache::has("progress");
           $where_progress=[
               "is_cancel"=>1,
           ];
           $with_progress = [
               "Product",
               "FirstType",
           ];
           if(empty($check)){
               $progress_model=Progress::with($with_progress)
                   ->whereNotNull("first_pay_time")
                   ->where("notice_times",">",0)
                   ->where($where_progress)
                   ->orderBy("id")
                   ->get();
               if(!empty($progress_model)){
                   $progress=$progress_model->toArray();
                   Cache::store( 'file' )->put( "progress",$progress,$expiresAt );
               }else{
                   return ;
               }

           }
           $progress    = Cache::store( 'file' )->get("progress");
           $noticeConfig=config( 'config.NOTICE_TIMES' );
           $Progress=new Progress();
           $notice_product=[];
           foreach ($progress as $key => $value){
               dump(date("Y-m-d H:i:s",time())."-authority_time-".$value['product']['authority_time']);
               if(!empty($value['product']['authority_time'])){
                   $notice_times= $value['notice_times'];
                   $first_type=$value['first_type']['name'];
                   $type_config=$noticeConfig[$first_type];
                   $nowtime= $Carbon->parse( $time,$timeZone );
                   $noticeTime= $Carbon->parse( $value['product']['authority_time'],$timeZone )->modify($type_config[$notice_times]);
                   $is_notice=$noticeTime->lt($nowtime);
                   if($is_notice){
                       $notice_product[]=$value[ 'product_id' ];
                       $progress[$key]['notice_times']--;
                       dump(date("Y-m-d H:i:s",time())."-product_id-".$value[ 'product_id' ]);
                       WechatController::updateProgressNotice( $value[ 'product_id' ] );
                       $product_save=$Progress->find($value[ 'id' ])->decrement("notice_times");
                   }
               }
           }

           Cache::store( 'file' )->put( "progress",$progress,$expiresAt );
       }catch (\Exception $e){
           return Response::show("0001",$e->getFile()."/n".$e->getLine()."/n".$e->getMessage()."/".$e->getCode());
       }

    }
}
